package com.hongxin.nw.netty.model.packing.info;

import com.hongxin.nw.netty.utils.BinaryTools;
import com.hongxin.nw.netty.utils.FieldPack;
import com.hongxin.nw.netty.values.StaticValue;
import com.hongxin.nw.netty.values.TypeIdentif;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;

/**
 * @remark A.8下发计费模型下行数据
 * @author: GuoXiaolin
 * @date 2019/5/28 10:17
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Getter
@Setter
public class Csdna1_6 implements IPacking  {

    private String co; //1	充电设备编号	压缩BCD码	8Byte	充电设备编号
    private String chargeIdentify; //2	充电接口标识	BIN码	1Byte	充电设备为一桩多充时用来标记接口号，一桩一充时此项为0。多个接口时顺序对每个接口进行编号
    private String billingModel; //3	计费模型ID	BIN码	8Byte	运营管理系统产生
    private String aotTime; //4	生效时间	BIN码	7Byte	CP56Time2a格式
    private String sotTime; //5	失效时间	BIN码	7Byte	CP56Time2a格式
    private String executeStatus; //6	执行状态	压缩BCD码 	2Byte	0001-有效 0002-无效
    private String meterType; //7	计量类型	压缩BCD码	  2Byte	0001-充电量
    private String TimeNumb; //8	时段数N	BIN码	1Byte	取值范围：0—12

    /**
     * tistr
     *9	第1个时段起始时间点	BIN码	2Byte	高字节：小时（0-24） 低字节：分钟（0-60）
     *10	第1个时段标志	BIN码	1Byte	1：尖时段；2：峰时段 3：平时段，4：谷时段
     *…… ……	 ……	……	取值范围：1—4
     *9N	第N个时段起始时间点	BIN码	2Byte	高字节：小时（0-24） 低字节：分钟（0-60）
     *9N+1	第N个时段标志	BIN码	1Byte	1：尖时段；2：峰时段 3：平时段，4：谷时段
     */
    private StringBuilder timeInfos = new StringBuilder();

    @Override
    public String packing() {
        return this.co +
                this.chargeIdentify +
                this.billingModel +
                this.aotTime +
                this.sotTime +
                this.executeStatus +
                this.meterType +
                this.TimeNumb +
                this.timeInfos.toString();


    }

    public Csdna1_6 setCo(String co) {
        this.co = FieldPack.caseToBCD(co, TypeIdentif.BYTE_CO.getByteLength());
        return this;
    }

    public Csdna1_6 setChargeIdentify(String chargeIdentify) {
        this.chargeIdentify = FieldPack.caseToBin(chargeIdentify, TypeIdentif.BYTE_CHARGE_INTERFACE_IDENTIFY.getByteLength());
        return this;
    }

    public Csdna1_6 setBillingModel(String billingModel) {
        this.billingModel = FieldPack.caseToBCD(billingModel, TypeIdentif.BYTE_BILLING_MODEL_ID.getByteLength());

        return this;
    }

    public Csdna1_6 setAotTime(String aotTime) {
        this.aotTime = FieldPack.caseTime(aotTime);
        return this;
    }

    public Csdna1_6 setSotTime(String sotTime) {
        this.sotTime = FieldPack.caseTime(sotTime);
        return this;
    }

    public Csdna1_6 setExecuteStatus(String executeStatus) {
        this.executeStatus = FieldPack.caseToBCD(executeStatus, TypeIdentif.BYTE_EXECUTE_STATUS.getByteLength());
        return this;
    }

    public Csdna1_6 setMeterType(String meterType) {
        this.meterType = FieldPack.caseToBCD(meterType, TypeIdentif.BYTE_METER_TYPE.getByteLength());
        return this;
    }

    public Csdna1_6 setTimeNumb(String timeNumb) {
        this.TimeNumb = FieldPack.caseToBin(timeNumb, TypeIdentif.BYTE_TIME_NUMB.getByteLength());
        return this;
    }

    public Csdna1_6 setTimeInfos(List<Map<String ,String>> list) {
        for (Map<String, String> map : list) {
            String[] ss = String.valueOf(map.get("startTime")).split(":");
            String startTime = BinaryTools.addZero(Integer.toHexString(Integer.valueOf(ss[0])), 2, StaticValue.DIRECTION_LEFT);
            startTime = BinaryTools.addZero(Integer.toHexString(Integer.valueOf(ss[1])), 2, StaticValue.DIRECTION_LEFT) + startTime;

            String identify = FieldPack.caseToBin(map.get("identify"), TypeIdentif.BYTE_TIME_NUMB.getByteLength()); //    第N个时段标志	BIN码	1Byte	1：尖时段；2：峰时段 3：平时段，4：谷时段
            String jEPrice = FieldPack.caseToBin(map.get("jEPrice"), TypeIdentif.BYTE_J_PRICE.getByteLength()); //9N+2	第N个时段尖电价	BIN码	4Byte	精确到小数点后五位
            String fEPrice = FieldPack.caseToBin(map.get("fEPrice"), TypeIdentif.BYTE_F_PRICE.getByteLength()); //9N+3	第N个时段峰电价	BIN码	4Byte	精确到小数点后五位
            String pEPrice = FieldPack.caseToBin(map.get("pEPrice"), TypeIdentif.BYTE_P_PRICE.getByteLength()); //9N+4	第N个时段平电价	BIN码	4Byte	精确到小数点后五位
            String gEPrice = FieldPack.caseToBin(map.get("gEPrice"), TypeIdentif.BYTE_G_PRICE.getByteLength()); //9N+5	第N个时段谷电价	BIN码	4Byte	精确到小数点后五位
            String serverPrice = FieldPack.caseToBin(map.get("serverPrice"), TypeIdentif.BYTE_SERVER_PRICE.getByteLength()); //9N+6	第N个服务费单价	BIN码	4Byte	精确到小数点后五位
            String occupancyPrice = FieldPack.caseToBin(map.get("occupancyPrice"), TypeIdentif.BYTE_OCCUPANCY_PRICE.getByteLength()); //9N+7	第N个占位费单价	BIN码	4Byte	精确到小数点后五位
            String subscriptionPrice = FieldPack.caseToBin(map.get("subscriptionPrice"), TypeIdentif.BYTE_SUBSCRIPTION_PRICE.getByteLength()); //9N+8	第N个预约费单价	BIN码	4Byte	精确到小数点后五位

            this.timeInfos.append(startTime)
                    .append(identify)
                    .append(jEPrice)
                    .append(fEPrice)
                    .append(pEPrice)
                    .append(gEPrice)
                    .append(serverPrice)
                    .append(occupancyPrice)
                    .append(subscriptionPrice);
        }
        return this;
    }

    @Override
    public String toString() {
        return "Csdna1_6{" +
                "co='" + co + '\'' +
                ", chargeIdentify='" + chargeIdentify + '\'' +
                ", billingModel='" + billingModel + '\'' +
                ", aotTime='" + aotTime + '\'' +
                ", sotTime='" + sotTime + '\'' +
                ", executeStatus='" + executeStatus + '\'' +
                ", meterType='" + meterType + '\'' +
                ", TimeNumb='" + TimeNumb + '\'' +
                '}';
    }
}
